package com.hkts.management.cargoViewRecord.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hkts.management.cargoViewRecord.entity.HktsCargoviewRecord;
import com.hkts.management.cargoViewRecord.service.ICargoViewRecordService;
import com.hkts.management.common.utils.Result;
import com.hkts.management.common.utils.ResultUtil;
import com.hkts.management.utils.CustomPageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yck
 * @since 2021-04-28
 */
@Api(tags = "看货记录信息接口")
@RestController
@RequestMapping("/order/cargoViewRecord")
public class CargoViewRecordController {
    private final Logger logger = LoggerFactory.getLogger(CargoViewRecordController.class);

    @Autowired
    public ICargoViewRecordService iCargoViewRecordService;

    /**
     * 分页查询数据
     *
     * @param page            分页信息
     * @param cargoViewRecord 查询条件
     * @return
     */
    @ResponseBody
    @GetMapping("/getCargoViewRecordPageList")
    public Result<Object> getHontUserList(Page<HktsCargoviewRecord> page, HktsCargoviewRecord cargoViewRecord) {
        return ResultUtil.data(CustomPageUtils.getCustomPage(iCargoViewRecordService.selectPage(page, cargoViewRecord)));
    }

    /**
     * 保存和修改公用的
     *
     * @param cargoViewRecord 传递的实体
     * @return saveOrUpdate
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/cargoViewRecordSave")
    public Result<Object> cargoViewRecordSave(HktsCargoviewRecord cargoViewRecord) {
        iCargoViewRecordService.saveOrUpdate(cargoViewRecord);
        return ResultUtil.success("保存成功");
    }

    /**
     * 根据id获取对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET, value = "/cargoViewRecordGetById")
    public Result<Object> cargoViewRecordGetById(String id) {
        HktsCargoviewRecord byId = iCargoViewRecordService.getById(id);
        return ResultUtil.data(byId);
    }

    /**
     * 根据id删除对象
     *
     * @param id 实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/cargoViewRecordDelete")
    public Result<Object> hontUserDelete(String id) {
        iCargoViewRecordService.removeById(id);
        return ResultUtil.success("删除成功");
    }

    /**
     * 批量删除对象
     *
     * @param item 实体集合ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/cargoViewRecordBatchDelete")
    public Result<Object> deleteBatchIds(String item) {
        List<String> ids = (List<String>) JSON.parse(item);
        iCargoViewRecordService.removeByIds(ids);
        return ResultUtil.success("删除成功");
    }
    /**
     * 根据货源id获取查看记录数
     */
    @ResponseBody
    @GetMapping("/getViewRecordCount")
    @ApiOperation(value = "根据货源id获取查看记录数")
    public Result<Object> getViewRecordCount(String cargoId){
           int count= iCargoViewRecordService.getViewRecordCount( cargoId);
           return ResultUtil.data(count);
    }
    /**
     * 添加查看记录
     * @param cargoViewRecord 传递的实体
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/addcargoViewRecord")
    @ApiOperation(value = "添加查看记录")
    public Result<Object> addcargoViewRecord(HktsCargoviewRecord cargoViewRecord) {
        int count=iCargoViewRecordService.addcargoViewRecord(cargoViewRecord);
        return ResultUtil.data(count);
    }
}
